<template>
  <el-dialog v-el-drag-dialog :title="!form.menuId?'新建菜单':'编辑菜单'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="menuForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-form-item hidden>
        <el-input v-model="form.menuId" hidden />
      </el-form-item>
      <el-form-item prop="parentId" hidden>
        <el-input v-model="form.parentId" hidden />
      </el-form-item>
      <el-card>
        <el-form-item label="上级菜单" prop="parentName">
          <el-input v-model="form.parentName" readonly>
            <el-button v-if="hasLoaded" slot="append" icon="el-icon-search" @click="selectMenu" />
          </el-input>
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="form.menuName" />
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-select v-model="form.menuType">
            <el-option label="目录" value="M" />
            <el-option label="菜单" value="C" />
            <el-option label="按钮" value="F" />
          </el-select>
        </el-form-item>
        <el-form-item label="请求地址" prop="path" :hidden="form.menuType==='F'">
          <el-input v-model="form.path" />
        </el-form-item>
        <el-form-item label="重定向地址" prop="redirect" :hidden="form.menuType==='F'">
          <el-input v-model="form.redirect" />
        </el-form-item>
        <el-form-item label="组件名称" prop="component" :hidden="form.menuType==='F'">
          <el-input v-model="form.component" />
        </el-form-item>
        <el-form-item label="打开方式" prop="target" :hidden="form.menuType==='F'">
          <el-radio v-model="form.target" label="default">默认</el-radio>
          <el-radio v-model="form.target" label="blank">新窗口</el-radio>
        </el-form-item>
        <el-form-item label="权限标识" prop="perms">
          <el-input v-model="form.perms" />
        </el-form-item>
        <el-form-item label="显示排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" :min="1" />
        </el-form-item>
        <el-form-item label="图标" prop="icon" :hidden="form.menuType==='F'">
          <div style="display:flex;">
            <icon-picker style="width:90%" />
            <i class="el-icon-delete" @click="form.icon=''" />
          </div>
        </el-form-item>
        <el-form-item label="状态" prop="visible">
          <el-select v-model="form.visible">
            <el-option
              v-for="item in optionOfStatus"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-card>
    </el-form>

    <menu-tree-dialog v-if="openTreeDialog" ref="treeDialog" @confirmDialog="setMenu" />

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { menuLoad, menuSave } from '@/api/system/menu'
import IconPicker from '@/components/IconPicker'
import MenuTreeDialog from '../tree/dialog'

export default {
  name: 'MenuFormDialog',
  components: {
    IconPicker,
    MenuTreeDialog
  },
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        menuId: undefined,
        parentId: undefined,
        parentName: '',
        menuName: '',
        menuType: undefined,
        path: undefined,
        redirect: undefined,
        component: undefined,
        target: 'default',
        perms: undefined,
        orderNum: 1,
        icon: undefined,
        visible: undefined
      },
      rules: {
        parentName: [{ required: true, message: '请选择上级菜单' }],
        menuName: [{ required: true, message: '请填写菜单名称' }],
        menuType: [{ required: true, message: '请选择菜单类型' }]
      },
      optionOfStatus: [],
      openTreeDialog: false
    }
  },
  methods: {
    init(menuId, parentId) {
      this.hasLoaded = false
      this.form.menuId = menuId || null
      this.form.parentId = parentId || null
      this.visible = true
      this.$nextTick(() => {
        this.$refs.menuForm.resetFields()
        this.loadForm(menuId, parentId)
        this.getDictData()
      })
    },
    loadForm(menuId, parentId) {
      menuLoad({
        'id': menuId,
        'pid': parentId
      }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['system:menu:edit'])
      }).catch(() => {})
    },
    getDictData() {
      this.$dictGroup('sys_show_hide').then(response => {
        this.optionOfStatus = response
      }).catch(e => {})
    },
    selectMenu() {
      this.openTreeDialog = true
      const selected = this.form.parentId
      this.$nextTick(() => {
        this.$refs.treeDialog.init(selected)
      })
    },
    setMenu(node) {
      this.form.parentId = node[0].id
      this.form.parentName = node[0].title
    },
    onSubmit() {
      this.$refs.menuForm.validate(valid => {
        if (valid) {
          menuSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.$refs.menuForm.resetFields()
            this.visible = false
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.menuForm.resetFields()
      this.visible = false
    }
  }
}
</script>
